Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
temp-write
Advanced tools
The temp-write npm package allows you to write temporary files to the filesystem. It is useful for scenarios where you need to create temporary files that are automatically cleaned up after use.
Write temporary file with content
This feature allows you to write a string to a temporary file and get the file path. The file is automatically cleaned up after the process exits.
const tempWrite = require('temp-write');
const fs = require('fs');
(async () => {
const filePath = await tempWrite('Hello, world!');
console.log(filePath);
console.log(fs.readFileSync(filePath, 'utf8'));
})();
Write temporary file with specific extension
This feature allows you to specify the extension of the temporary file. The file is automatically cleaned up after the process exits.
const tempWrite = require('temp-write');
const fs = require('fs');
(async () => {
const filePath = await tempWrite('Hello, world!', { extension: 'txt' });
console.log(filePath);
console.log(fs.readFileSync(filePath, 'utf8'));
})();
Write temporary file with specific name
This feature allows you to specify the name of the temporary file. The file is automatically cleaned up after the process exits.
const tempWrite = require('temp-write');
const fs = require('fs');
(async () => {
const filePath = await tempWrite('Hello, world!', { name: 'example.txt' });
console.log(filePath);
console.log(fs.readFileSync(filePath, 'utf8'));
})();
The tmp package provides similar functionality for creating temporary files and directories. It offers more control over the lifecycle of the temporary files and directories, including manual cleanup.
The temp package is another alternative for handling temporary files and directories. It provides a more comprehensive API for managing temporary resources, including automatic cleanup and custom directory support.
The fs-extra package extends the native fs module with additional methods, including methods for handling temporary files. It provides a more extensive set of file system utilities beyond just temporary file handling.
Write string/buffer/stream to a random temp file
$ npm install temp-write
const fs = require('fs');
const tempWrite = require('temp-write');
const filePath = tempWrite.sync('unicorn');
//=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b'
fs.readFileSync(filePath, 'utf8');
//=> 'unicorn'
tempWrite.sync('unicorn', 'pony.png');
//=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/pony.png'
tempWrite.sync('unicorn', 'rainbow/cake/pony.png');
//=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/rainbow/cake/pony.png'
Returns a Promise
for the file path of the temp file.
Returns the file path of the temp file.
Type: string | Buffer | stream.Readable
Data to write to the temp file. Streams are supported only with the async API.
Type: string
Examples: 'img.png'
'foo/bar/baz.png'
Optionally supply a file path which is appended to the random path.
MIT © Sindre Sorhus
FAQs
Write data to a random temporary file
The npm package temp-write receives a total of 468,000 weekly downloads. As such, temp-write popularity was classified as popular.
We found that temp-write demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.